package org.gten.gegedexin.controller;

import org.gten.gegedexin.pojo.UserPojo;
import org.gten.gegedexin.service.TokenServiceImpl;
import org.gten.gegedexin.service.UserServiceImpl;
import org.gten.gegedexin.service.user.common.ResResult;
import org.gten.gegedexin.service.user.common.Result;
import org.gten.gegedexin.service.user.common.ResultCode;
import org.gten.gegedexin.service.user.common.User;
import org.gten.gegedexin.service.user.token.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class AccountController {

    int limit = 3000;

    @Autowired
    TokenServiceImpl tokenService;

    @Autowired
    UserServiceImpl userService;

    @PostMapping("/login")

    public Result login (@RequestBody User user){
        System.out.println(LocalDateTime.now()+":"+"[用户登录]"+user.getAccount()+"请求登录。");
        boolean is_exist = userService.isUserNameExist(user.getAccount());
        if(!is_exist) {
            System.out.println(LocalDateTime.now()+":"+"[用户登录]"+user.getAccount()+"账号未找到！");
            return ResResult.fail(ResultCode.ACCOUNT_NOT_FOUND);
        }UserPojo userPojo = userService.getByUsername(user.getAccount());
        if(!user.getPassword().equals(userPojo.getPassword())) {
            System.out.println(LocalDateTime.now()+":"+"[用户登录]"+user.getAccount()+"密码不正确！");
            return ResResult.fail(ResultCode.PASSWORD_INCORRECT);
        }
        String Token = JwtToken.createToken(user);
//        redisTemplate.boundValueOps(Constant.REDIS_USER_PREFIX.getClaim()+Token).set(user,30, TimeUnit.MINUTES);

        if(tokenService.getAccount(Token)!=null)    tokenService.updateTime(Token);
        else tokenService.addToken(user.getAccount(), Token, LocalDateTime.now(), limit);
        System.out.println(LocalDateTime.now()+":"+"[用户登录]"+user.getAccount()+"登录成功。");
        return ResResult.suc(Token);//返回token
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        System.out.println(LocalDateTime.now()+":"+"[用户注册]"+user.getAccount()+"请求注册。");
        boolean is_exist = userService.isUserNameExist(user.getAccount());
        System.out.println(user.getAccount());
        System.out.println(user.getPassword());
        System.out.println(user.getName());
        if(is_exist) {
            System.out.println(LocalDateTime.now()+":"+"[用户注册]"+user.getAccount()+"账号已存在！");
            return ResResult.fail(ResultCode.ACCOUNT_EXISTED);
        }userService.addUser(user.getAccount(),user.getPassword(),user.getName());
        String Token = JwtToken.createToken(user);
        System.out.println(Token);
//        redisTemplate.boundValueOps(Constant.REDIS_USER_PREFIX.getClaim()+Token).set(user,30, TimeUnit.MINUTES);

        System.out.println(user.getAccount());
        tokenService.addToken(user.getAccount(), Token, LocalDateTime.now(), limit);
        System.out.println(LocalDateTime.now()+":"+"[用户注册]"+user.getAccount()+"注册成功。");
        return ResResult.suc(Token);//返回token
    }

    @PostMapping("/delete")
    public Result delete(User user){
        if(!(userService.isUserNameExist(user.getAccount()))) return ResResult.fail(ResultCode.ACCOUNT_NOT_FOUND);
        UserPojo userPojo = userService.getByUsername(user.getAccount());
        if(!user.getPassword().equals(userPojo.getPassword())) return ResResult.fail(ResultCode.PASSWORD_INCORRECT);
        userService.delUser(userPojo.getUsername());
        return ResResult.suc("账号已删除。");
    }

    @GetMapping(value="/getResult")
    public Result getResult(){
        return ResResult.suc("test");
    }
}
